package com.phiture.erp.basic.web.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import com.phiture.erp.basic.core.pojo.dto.ErpMaterialPurchasingPageReqDTO;
import com.phiture.erp.basic.core.pojo.dto.ErpMaterialPurchasingSaveReqDTO;
import com.phiture.erp.basic.core.pojo.vo.ErpMaterialPurchasingRespVO;
import com.phiture.erp.basic.core.service.ErpMaterialPurchasingService;
import com.phiture.erp.basic.dal.entity.ErpMaterialPurchasingDO;
import com.phiture.erp.common.enums.ErpMaterialPurchasingCheckTypeEnum;
import com.phiture.erp.common.enums.ErpMaterialPurchasingStatusEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 物料采购属性")
@RestController
@RequestMapping("/erp/material-purchasing")
public class ErpMaterialPurchasingController {

    @Resource
    private ErpMaterialPurchasingService materialPurchasingService;

    @PostMapping("/create")
    @Operation(summary = "创建物料采购属性")
    @PreAuthorize("@ss.hasPermission('erp:material-purchasing:create')")
    public CommonResult<Long> createMaterialPurchasing(@Valid @RequestBody ErpMaterialPurchasingSaveReqDTO createReqDTO) {
        return success(materialPurchasingService.createMaterialPurchasing(createReqDTO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料采购属性")
    @PreAuthorize("@ss.hasPermission('erp:material-purchasing:update')")
    public CommonResult<Boolean> updateMaterialPurchasing(@Valid @RequestBody ErpMaterialPurchasingSaveReqDTO updateReqDTO) {
        materialPurchasingService.updateMaterialPurchasing(updateReqDTO);
        return success(true);
    }

    @PutMapping("/createOrUpdate")
    @Operation(summary = "创建或者更新物料采购属性")
    @PreAuthorize("@ss.hasPermission('erp:material-purchasing:update')")
    public CommonResult<Boolean> crateOrUpdateMaterialPurchasing(@Valid @RequestBody ErpMaterialPurchasingSaveReqDTO reqDTO) {
        materialPurchasingService.createOrUpdateMaterialPurchasing(reqDTO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料采购属性")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:material-purchasing:delete')")
    public CommonResult<Boolean> deleteMaterialPurchasing(@RequestParam("id") Long id) {
        materialPurchasingService.deleteMaterialPurchasing(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料采购属性")
    @Parameter(name = "materialId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:material-purchasing:query')")
    public CommonResult<ErpMaterialPurchasingRespVO> getMaterialPurchasing(@RequestParam("materialId") Long materialId) {
        return success(materialPurchasingService.getMaterialPurchasingByMaterialId(materialId));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料采购属性分页")
    @PreAuthorize("@ss.hasPermission('erp:material-purchasing:query')")
    public CommonResult<PageResult<ErpMaterialPurchasingRespVO>> getMaterialPurchasingPage(@Valid ErpMaterialPurchasingPageReqDTO pageReqDTO) {
        PageResult<ErpMaterialPurchasingDO> pageResult = materialPurchasingService.getMaterialPurchasingPage(pageReqDTO);
        return success(
                BeanUtils.toBean(
                        pageResult,
                        ErpMaterialPurchasingRespVO.class,
                        info ->
                        info.setStatusDesc(ErpMaterialPurchasingStatusEnum.convert(info.getStatus()))
                                .setPurchaseCheckTypeDesc(ErpMaterialPurchasingCheckTypeEnum.convert(info.getPurchaseCheckType()))
                )
        );
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料采购属性 Excel")
    @PreAuthorize("@ss.hasPermission('erp:material-purchasing:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialPurchasingExcel(@Valid ErpMaterialPurchasingPageReqDTO pageReqDTO, HttpServletResponse response) throws IOException {
        pageReqDTO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpMaterialPurchasingDO> list = materialPurchasingService.getMaterialPurchasingPage(pageReqDTO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料采购属性.xls", "数据", ErpMaterialPurchasingRespVO.class,
                        BeanUtils.toBean(list, ErpMaterialPurchasingRespVO.class, info -> info.setStatusDesc(ErpMaterialPurchasingStatusEnum.convert(info.getStatus()))
                                .setPurchaseCheckTypeDesc(ErpMaterialPurchasingCheckTypeEnum.convert(info.getPurchaseCheckType()))));
    }

}